<template>
	<view class="login-container">
		<view class="smart-panel-h" @click="goDetailPage('register')"></view>
		<view class="login-form">
			<text class="title">注册</text>
			<view class="input-item">
				<input type="text" v-model="username" placeholder="请输入账号或用户名" />
			</view>
			<view class="input-item">
				<input type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<button class="login-btn" @click="handleLogin">注册</button>
			<navigator url="/pages/components/logins/logins" class="register-link">已有账号?登录</navigator>
		</view>
	</view>
</template>

<script>
	import {
		nextTick
	} from 'vue';

	export default {
		data() {
			return {
				username: '',
				password: ''
			};
		},
		methods: {
			handleLogin() {
				if (this.username && this.password) {
					uni.showToast({
						title: '注册成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.setTabBarItem({
							index: 3,
							text: '我的',
							iconPath: '/static/tab/my.png',
							selectedIconPath: '/static/tab/my.png'
						})
					}, 300)
					uni.switchTab({
						url: '/pages/TabBar/tabcompage/tabcompage'
					})
				} else {
					uni.showToast({
						title: '内容不能为空',
						icon: 'none'
					});
					return;
				}
				//UNICLOUD
				uniCloud.callFunction({
						name: 'addUser',
						data: {
							'username': this.username,
							'password': this.password
						},
						success: (msg) => {
							console.log(JSON.stringify(msg));
							setTimeout(() => {
								uni.hideLoading()
								uni.showToast({
									titile: '注册成功'
								})
								uni.navigateBack();
								uni.setTabBarItem({
									index: 3,
									text: '我的',
									iconPath: '/static/tab/my.png',
									selectedIconPath: '/static/tab/my.png'
								})
							}, 1000)
						},
						fail: (error) => {
							console.error(error);
						}
				})
			}
		}
	};
</script>

<style>

</style>